{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业\n",
    "1. 对数据做数据探索分析（可参考EDA_BikeSharing.ipynb，不计分）\n",
    "2. 适当的特征工程（可参考FE_BikeSharing.ipynb，不计分）\n",
    "3. 对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据，评价指标为RMSE。（10分）\n",
    "4. 用训练数据训练最小二乘线性回归模型（20分）、岭回归模型、Lasso模型，其中岭回归模型（30分）和Lasso模型（30分），注意岭回归模型和Lasso模型的正则超参数调优。\n",
    "5. 比较用上述三种模型得到的各特征的系数，以及各模型在测试集上的性能。并简单说明原因。（10分） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入工具包\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.linear_model import LinearRegression,RidgeCV,LassoCV,ElasticNetCV\n",
    "\n",
    "from sklearn.model_selection import train_test_split,KFold\n",
    "\n",
    "# 模型评估\n",
    "from sklearn.metrics import mean_squared_error,r2_score\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>yr_0</th>\n",
       "      <th>yr_1</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>...</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.171000</td>\n",
       "      <td>0.144830</td>\n",
       "      <td>0.449638</td>\n",
       "      <td>0.465740</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.175530</td>\n",
       "      <td>0.174649</td>\n",
       "      <td>0.607131</td>\n",
       "      <td>0.284297</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.209120</td>\n",
       "      <td>0.197158</td>\n",
       "      <td>0.449313</td>\n",
       "      <td>0.339143</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  instant  season_1  season_2  season_3  season_4  yr_0  yr_1  \\\n",
       "0           0        1         1         0         0         0     1     0   \n",
       "1           1        2         1         0         0         0     1     0   \n",
       "2           2        3         1         0         0         0     1     0   \n",
       "3           3        4         1         0         0         0     1     0   \n",
       "4           4        5         1         0         0         0     1     0   \n",
       "\n",
       "   mnth_1  mnth_2  ...   weathersit_1  weathersit_2  weathersit_3      temp  \\\n",
       "0       1       0  ...              0             1             0  0.355170   \n",
       "1       1       0  ...              0             1             0  0.379232   \n",
       "2       1       0  ...              1             0             0  0.171000   \n",
       "3       1       0  ...              1             0             0  0.175530   \n",
       "4       1       0  ...              1             0             0  0.209120   \n",
       "\n",
       "      atemp       hum  windspeed  holiday  workingday   cnt  \n",
       "0  0.373517  0.828620   0.284606        0           0   985  \n",
       "1  0.360541  0.715771   0.466215        0           0   801  \n",
       "2  0.144830  0.449638   0.465740        0           1  1349  \n",
       "3  0.174649  0.607131   0.284297        0           1  1562  \n",
       "4  0.197158  0.449313   0.339143        0           1  1600  \n",
       "\n",
       "[5 rows x 30 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入训练数据\n",
    "train=pd.read_csv('G:/VS_AI/homework/LinearRegression/FE_day.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 731 entries, 0 to 730\n",
      "Data columns (total 30 columns):\n",
      "Unnamed: 0      731 non-null int64\n",
      "instant         731 non-null int64\n",
      "season_1        731 non-null int64\n",
      "season_2        731 non-null int64\n",
      "season_3        731 non-null int64\n",
      "season_4        731 non-null int64\n",
      "yr_0            731 non-null int64\n",
      "yr_1            731 non-null int64\n",
      "mnth_1          731 non-null int64\n",
      "mnth_2          731 non-null int64\n",
      "mnth_3          731 non-null int64\n",
      "mnth_4          731 non-null int64\n",
      "mnth_5          731 non-null int64\n",
      "mnth_6          731 non-null int64\n",
      "mnth_7          731 non-null int64\n",
      "mnth_8          731 non-null int64\n",
      "mnth_9          731 non-null int64\n",
      "mnth_10         731 non-null int64\n",
      "mnth_11         731 non-null int64\n",
      "mnth_12         731 non-null int64\n",
      "weathersit_1    731 non-null int64\n",
      "weathersit_2    731 non-null int64\n",
      "weathersit_3    731 non-null int64\n",
      "temp            731 non-null float64\n",
      "atemp           731 non-null float64\n",
      "hum             731 non-null float64\n",
      "windspeed       731 non-null float64\n",
      "holiday         731 non-null int64\n",
      "workingday      731 non-null int64\n",
      "cnt             731 non-null int64\n",
      "dtypes: float64(4), int64(26)\n",
      "memory usage: 171.4 KB\n"
     ]
    }
   ],
   "source": [
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将数据化成数组形式\n",
    "X=train.drop(['cnt'],axis=1)\n",
    "y=train['cnt']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "trian samples: (584, 29)\n"
     ]
    }
   ],
   "source": [
    " X_train, X_test, y_train, y_test = train_test_split(\n",
    "     X, y, test_size=0.2, random_state=0)\n",
    "print(\"trian samples:\",X_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    }
   ],
   "source": [
    "# 保存测试ID，用于结果提交\n",
    "testID=X_test[\"instant\"]\n",
    "\n",
    "X_train.drop(['instant'],axis=1,inplace=True) #表示在原空间文件做操作\n",
    "X_test.drop(['instant'],axis=1,inplace=True)\n",
    "\n",
    "feat_name=X_train.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、最小二乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RMSE on Triaining set : 759.9761803277083\n",
      "RMSE on Test set : 802.2025420987427\n",
      "R2_Score on Triaining set : 0.8404523644237587\n",
      "R2_Score on Test set : 0.8486933204464746\n"
     ]
    }
   ],
   "source": [
    "lr=LinearRegression()\n",
    "lr.fit(X_train,y_train)\n",
    "\n",
    "y_train_pred=lr.predict(X_train)\n",
    "y_test_pred=lr.predict(X_test)\n",
    "\n",
    "rmse_train=np.sqrt(mean_squared_error(y_train,y_train_pred))\n",
    "rmse_test=np.sqrt(mean_squared_error(y_test,y_test_pred))\n",
    "print(\"RMSE on Triaining set :\",rmse_train)\n",
    "print(\"RMSE on Test set :\",rmse_test)\n",
    "\n",
    "r2_score_train=r2_score(y_train,y_train_pred)\n",
    "r2_score_test=r2_score(y_test,y_test_pred)\n",
    "print(\"R2_Score on Triaining set :\",r2_score_train)\n",
    "print(\"R2_Score on Test set :\",r2_score_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(28,)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr.coef_.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小二乘线性回归选择了28个特征；消除了0特征\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAacAAAD8CAYAAADT0WsYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0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hzdw36MY2LA3iIxHxIqT/wAMfzuXd/fdYN/ItrBGk2UXT9E/SDpKWAy+TBs1n6Ph354/9yJ+/Tvpdq4v+9ZrnnGpJ0r+TktLbuyYi/i3XuQZ4F5jedliR+kHx/6GITupXVCl9K3ZYkbK669t2aLT2lqujftZ1/yXtCvwr8KWI+C91nMHZcP2LiD8Aw/N31w+Sbq1vUy3/s67758GpCiJibGef5y9UTyWlVbT9JehsK49i5RtJtx3em/8vqCpbf3TVtw40RN+2Q022YamglyT1j4gX822tl3N5R/1cD4xpVz6nCu3skqQdSQPT9Ih4IBc3Tf/aRMRrkuaQvnPq6HenrX/rJb0X2J10S7cu/v76tl6NSToJuAo4LSJ+V/DRQ8A5eUXNfsABwCI62OIjD2o/A8bl48cDHc1caq2Z+wbNtw3LQ+Rdptn6z/4h4LN5VdvHgdfzbbEZwAmSPphXvp1A6VvYVEz+PuV7wBMR8c2Cj5qlf/3yjAlJOwNjSd+rdfS7U9jvccDs/LvW0e9ndVV7BYZ/tllhs5Z0f3d5/rm14LNrSPeMnwQ+WVB+Mmml0TOk22dt5fuT/hKtBe4Ddqpx384g/V/YJuAlYEaz9K2EvhftR73/APcCLwLv5H93F5G+h5gFPJ3/+aFcV8BNuY+tbL0i88L872otcEGt+5Xb9AnS7amVBb9vJzdR/w4l7QC+ElgFXJfLi/7uAH3y+7X58/0LzlX097OaP06IMDOzuuPbemZmVnc8OJmZWd3x4GRmZnXHg5OZmdUdD05mZlZ3PDiZmVnd8eBkZmZ1x4OTmZnVnf8PiGJmfBZukREAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 新添加打印\n",
    "coefs=pd.Series(lr.coef_,index=feat_name)\n",
    "print('最小二乘线性回归选择了'+str(sum(coefs!=0))+'个特征；消除了'+str(sum(coefs==0))+'特征')\n",
    "\n",
    "# 选择最重要的20个系数\n",
    "imp_coefs=pd.concat([coefs.sort_values().head(10),coefs.sort_values().tail(10)]) # 选择的是index+data???\n",
    "imp_coefs.plot(kind=\"barh\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 分析\n",
    "1、由上分析，OLS对训练数据比测试数据拟合的较好\n",
    "2、temp对cnt的影响最大\n",
    "3、参数最大的系数达到了3000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、岭回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "best alpha : 1.0\n",
      "cv of rmse[ 805.40692218  802.9204124   802.04450015  833.09390746 1008.13655374\n",
      " 1341.53244363]\n",
      "RMSE on Triaining set : 764.4405867564539\n",
      "RMSE on Test set : 794.1149965105627\n",
      "R2_Score on Triaining set : 0.8385723644066676\n",
      "R2_Score on Test set : 0.8517287912494265\n"
     ]
    }
   ],
   "source": [
    "# 设置超参数\n",
    "alphas=[0.01,0.1,1,10,100,1000]\n",
    "ridge=RidgeCV(alphas=alphas,store_cv_values=True) # store_cv_values:存储训练结果\n",
    "\n",
    "ridge.fit(X_train,y_train)\n",
    "\n",
    "alpha=ridge.alpha_\n",
    "print('best alpha :',alpha)\n",
    "\n",
    "# 交叉验证估计的预测误差\n",
    "mse_cv=np.mean(ridge.cv_values_,axis=0)\n",
    "rmse_cv=np.sqrt(mse_cv)\n",
    "print('cv of rmse'+str(rmse_cv))\n",
    "\n",
    "y_train_pred=ridge.predict(X_train)\n",
    "rmse_train=np.sqrt(mean_squared_error(y_train,y_train_pred))\n",
    "\n",
    "y_test_pred=ridge.predict(X_test)\n",
    "rmse_test=np.sqrt(mean_squared_error(y_test,y_test_pred))\n",
    "\n",
    "print(\"RMSE on Triaining set :\",rmse_train)\n",
    "print(\"RMSE on Test set :\",rmse_test)\n",
    "\n",
    "r2_score_train=r2_score(y_train,y_train_pred)\n",
    "r2_score_test=r2_score(y_test,y_test_pred)\n",
    "print(\"R2_Score on Triaining set :\",r2_score_train)\n",
    "print(\"R2_Score on Test set :\",r2_score_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小二乘线性回归选择了28个特征；消除了0特征\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAacAAAD8CAYAAADT0WsYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3X+8VVWd//HXOzPxV+GvilJEUSMVRbmijtL4g9FSyxz1668psSa/Vub0bbQYdYr5On7HhprGyh+j5s+YajQ1zRIZDCFCBOTChfC3NJKMiqbpZGTy+f6x1o3D4Zx7z72cc/Y5976fj8d9sM/aa+/92dcji7X22p+liMDMzKyVvKXoAMzMzMq5cTIzs5bjxsnMzFqOGyczM2s5bpzMzKzluHEyM7OW48bJzMxajhsnMzNrOW6czMys5by16ADa1fbbbx8jRowoOgwzs7aycOHC1RGxQ2/13Dj104gRI1iwYEHRYZiZtRVJv6qlXtsO60kaKukzRcdhZmb11849p6HAZ4Ariw7ErN5m3D+y6BDMqjryiCcbfo227TkBlwEjJXVKmiLpAknzJS2R9A8AkkZIekTSdZKWSpoqaYKkOZIelzQu15ss6RZJ9+fyTxV6Z2Zmg1w7N06TgCcjYgwwHdgdGAeMAcZK+kCutxtwObAPMAo4HTgUOB+4sOR8+wDHAgcDX5b0nmbchJmZbaidG6dSR+WfRcDDpEZo97zv6Yjoioi1wDJgRqRFrLqAESXn+FFEvB4Rq4GfkRq69Ug6W9ICSQteeOGFxt2Nmdkg187PnEoJ+KeI+Lf1CqURwJqSorUln9ey/v2Xr7q4wS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n0X5XgeJc8ezOXHkGb4PEkaGuwu35X0P9gTwK3AZg38Hd8CdAFL8v88w/obd5O/G4XHUBbPrvn7uDh/Ny/q6XtHGsq5IsffRckMzwbF9z3ScPgb+Xv6yf7ERhpKeyL/nNXEWFvuewocShp+WwJ05p9jmv17dYYIMzNrOR7WMzOzluPGyczMWo4bJzMzazlunMzMrOW4cTIzs5bjxsnMzFqOGyczM2s5bpzMzKzl/H/RNGh3K2CxwwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 新添加打印\n",
    "coefs=pd.Series(ridge.coef_,index=feat_name)\n",
    "print('最小二乘线性回归选择了'+str(sum(coefs!=0))+'个特征；消除了'+str(sum(coefs==0))+'特征')\n",
    "\n",
    "# 选择最重要的20个系数\n",
    "imp_coefs=pd.concat([coefs.sort_values().head(10),coefs.sort_values().tail(10)]) # 选择的是index+data???\n",
    "imp_coefs.plot(kind=\"barh\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分析：\n",
    "1、岭回归不论是在测试集上还是在训练集上的表现均优于最小二乘模型\n",
    "2、岭回归的系数中分别是温度和体感温度对cnt影响最大。湿度的负影响最大\n",
    "3、岭回归中的各个参数的系数均小于最小二乘模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、Lasso模型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "best alpha 252.93739944173396\n",
      "cv of rmse: 2052649.1806218196\n",
      "最小二乘线性回归选择了2个特征；消除了26特征\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAacAAAD8CAYAAADT0WsYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xu8XNP9//HXu6g7odJ+ERF1qYpLNAf1RX9xKUpLfVEtVaGtKurX9kGr1S/6U7/S6KPVC5qquvaqtFFtQqMR0iAJuSJu4SflW6GlFHHJ5/fHWqdnMplzzsycObNnJu/n43Ee9qy99p61Zngsa8/a762IwMzMrJW8pegGmJmZlfPgZGZmLceDk5mZtRwPTmZm1nI8OJmZWcvx4GRmZi3Hg5OZmbUcD05mZtZyPDiZmVnLWbXoBrSrjTbaKEaMGFF0M8zM2sqsWbOejYih/dXz4FSnESNGMHPmzKKbYWbWViQ9UU29qi/rSRohaX5Z2bmSTq+1cc1UbRslfUXSI5IWSjqgGW0zM7PKPHMCJG0HfBQYCWwC/EnSNhHxZrEtMzMr1uTbtlyhbN99Hh30923YgghJUyRdKOkeSQ9J2iuXj5V0g6SJkh6W9K2SYy6VNFPSAklfLyl/XNL/lTQ973+PpEmSHpV0Ukm9MyTNkDS37Piz8gzoT8C7qmj+ocAvImJpRCwCHgF2bcDHYmZmdWj0zGnViNhV0kHAOcB+uXwUsDOwFFgo6fsR8SRwVkT8XdIqwGRJO0bE3HzMkxGxu6TvAFcCewBrAAuAyyTtD2xNGkQETJD0PuBfpFnQzrl/9wKzALoHtoi4rKzdmwJ3lbxenMvMzKwAtQxOvT34qbT8hvzPWcCIkvLJEfECgKT7gc2BJ4GPSDoxt2NjYDuge3CakP85D1gnIl4EXpT0qqQhwP75775cbx3SYLUucGNEvJzfr/s8lQalbqqmv7mtJwI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vhfYX/vkW/iGszH/Ab4D5wFzgJmDTXC7gh6TVMvPK/mM9gfQj6iPA8QW3/xHSdenZ+a97teHhpEefzAHuBT5UckxX7vOjwA/IN4K3Wh/yvrNyOxdSspqNtJryobzvrBb49+gw0v/pLgX+Bkxqw++hYh/a6Xso6881+b/ducCEssGqYn9a7a/oz9cJEWZm1nK8Ws/MzFqOByczM2s5HpzMzKzleHAyM7OW48HJzMxajgcnMzNrOR6czMys5XhwMjOzlvP/Ae1CfTPeaOSsAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RMSE on Triaining set : 1424.174169095853\n",
      "RMSE on Test set : 1519.8515295649968\n",
      "R2_Score on Triaining set : 0.4397051631479437\n",
      "R2_Score on Test set : 0.45688429313302625\n"
     ]
    }
   ],
   "source": [
    "lasso=LassoCV()\n",
    "lasso.fit(X_train,y_train)\n",
    "alpha=lasso.alpha_\n",
    "print('best alpha',alpha)\n",
    "\n",
    "mse_cv=np.mean(lasso.mse_path_,axis=1)\n",
    "rmse_cv=np.sqrt(mse_cv)\n",
    "print('cv of rmse:',min(mse_cv))\n",
    "\n",
    "# 新添加打印\n",
    "coefs=pd.Series(lasso.coef_,index=feat_name)\n",
    "print('最小二乘线性回归选择了'+str(sum(coefs!=0))+'个特征；消除了'+str(sum(coefs==0))+'特征')\n",
    "\n",
    "# 选择最重要的20个系数\n",
    "imp_coefs=pd.concat([coefs.sort_values().head(10),coefs.sort_values().tail(10)]) # 选择的是index+data???\n",
    "imp_coefs.plot(kind=\"barh\")\n",
    "plt.show()\n",
    "\n",
    "\n",
    "y_train_pred=lasso.predict(X_train)\n",
    "rmse_train=np.sqrt(mean_squared_error(y_train,y_train_pred))\n",
    "\n",
    "y_test_pred=lasso.predict(X_test)\n",
    "rmse_test=np.sqrt(mean_squared_error(y_test,y_test_pred))\n",
    "\n",
    "print(\"RMSE on Triaining set :\",rmse_train)\n",
    "print(\"RMSE on Test set :\",rmse_test)\n",
    "\n",
    "r2_score_train=r2_score(y_train,y_train_pred)\n",
    "r2_score_test=r2_score(y_test,y_test_pred)\n",
    "print(\"R2_Score on Triaining set :\",r2_score_train)\n",
    "print(\"R2_Score on Test set :\",r2_score_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 分析：\n",
    "由上得到本次模型lasso甚至表现得不如OLS，尚未找到问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 提交文件，本次提交岭回归的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_test_pred=ridge.predict(X_test)\n",
    "\n",
    "#生成提交测试结果\n",
    "df=pd.DataFrame({'instant':testID,'cnt':y_test_pred})\n",
    "df.to_csv('submission.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 147 entries, 196 to 239\n",
      "Data columns (total 2 columns):\n",
      "instant    147 non-null int64\n",
      "cnt        147 non-null float64\n",
      "dtypes: float64(1), int64(1)\n",
      "memory usage: 3.4 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
